﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.Linq;
using QuanLyShopThoiTrang_DAO;

namespace QuanLyShopThoiTrang_DAO_WS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class QLSTT_DAO : System.Web.Services.WebService
    {

        [WebMethod]
        public bool SanPham_Them(SanPhamDTO sp)
        {
            return SanPhamDAO.SanPham_Them(sp);
            //try
            //{
            //    QLSQADataContext db = new QLSQADataContext();

            //    db.SanPhams.InsertOnSubmit(sp);

            //    db.SubmitChanges();
            //}
            //catch(Exception ex)
            //{
            //    return false;
            //}
            //return true;
        }

        [WebMethod]
        public bool SanPham_Xoa(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                SanPham s = db.SanPhams.Single(p => p.MaSanPham == sp.MaSanPham);

                s.SanPham_TrangThai = "Khong su dung";

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool SanPham_CapNhat(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                SanPham s = db.SanPhams.Single(p => p.MaSanPham == sp.MaSanPham);

                s.MaLoaiSanPham = sp.MaLoaiSanPham;
                s.MaNhaCungCap = sp.MaNhaCungCap;
                s.SanPham_GiaKhiBan = sp.SanPham_GiaKhiBan;
                //s.SanPham_GiaKhiMua = sp.SanPham_GiaKhiMua;
                s.SanPham_GiaUuDai = sp.SanPham_GiaUuDai;
                s.SanPham_HinhAnh = sp.SanPham_HinhAnh;
                s.SanPham_KichCo = sp.SanPham_KichCo;
                s.SanPham_MaHang = sp.SanPham_MaHang;
                s.SanPham_NhanHieu = sp.SanPham_NhanHieu;
                s.SanPham_SoLuong = sp.SanPham_SoLuong;
                s.SanPham_Ten = sp.SanPham_Ten;
                s.SanPham_XuatXu = sp.SanPham_XuatXu;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<SanPham> SanPham_TraCuu(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                if (sp.MaSanPham == -1)
                {
                    var listSP = from p in db.SanPhams
                                 where p.SanPham_TrangThai == "Su dung"
                                 select p;
                    return listSP.ToList();
                }

                if (sp.MaSanPham == -2)
                {
                    var listSP = from p in db.SanPhams
                                 where p.SanPham_TrangThai == "Su dung" & p.SanPham_SoLuong > 0
                                 select p;
                    return listSP.ToList();
                }

                else
                {
                    var listSP = from p in db.SanPhams
                                 where p.SanPham_TrangThai == "Su dung"
                                 select p;
                    return listSP.ToList();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public SanPham SanPham_LaySP(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                SanPham s = db.SanPhams.Single(p => p.SanPham_MaHang == sp.SanPham_MaHang);

                return s;

            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public SanPham SanPham_LaySPTheoMaSP(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                SanPham s = db.SanPhams.Single(p => p.MaSanPham == sp.MaSanPham);

                return s;

            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public bool SanPham_KiemTraMaSP(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listSP = from p in db.SanPhams
                             where p.SanPham_MaHang == sp.SanPham_MaHang
                             select p;

                if (listSP.Count() > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                return true;
            }
            return true;
        }

        [WebMethod]
        public LoaiSanPham SanPham_LayLoaiSP(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                LoaiSanPham lsp = db.LoaiSanPhams.First(p => p.MaLoaiSanPham == sp.MaLoaiSanPham);

                return lsp;
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public NhaCungCap SanPham_LayNCC(SanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhaCungCap lsp = db.NhaCungCaps.First(p => p.MaNhaCungCap == sp.MaNhaCungCap);

                return lsp;
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public bool LoaiSanPham_Them(LoaiSanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.LoaiSanPhams.InsertOnSubmit(sp);

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool LoaiSanPham_Xoa(LoaiSanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                LoaiSanPham s = db.LoaiSanPhams.Single(p => p.MaLoaiSanPham == sp.MaLoaiSanPham);

                s.LoaiSanPham_TrangThai = "Khong su dung";

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool LoaiSanPham_CapNhat(LoaiSanPham sp)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                LoaiSanPham s = db.LoaiSanPhams.Single(p => p.MaLoaiSanPham == sp.MaLoaiSanPham);

                s.LoaiSanPham_Ten = sp.LoaiSanPham_Ten;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<LoaiSanPham> LoaiSanPham_LayDanhSach()
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listSP = from p in db.LoaiSanPhams
                             where p.LoaiSanPham_TrangThai == "Su dung"
                             select p;

                return listSP.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

       

        /*[WebMethod]
        public int LoaiSanPham_LayMaLoaiSP()
        {
            QLSQADataContext db = new QLSQADataContext();
            int i;
            if ((from p in db.LoaiSanPhams select p.MaLoaiSanPham).Count() > 0)
            {
                i = (from p in db.LoaiSanPhams
                     select p.MaLoaiSanPham).Max();
            }
            else
                i = 0;
            return i + 1;
        }*/


        [WebMethod]
        public bool NhaCungCap_Them(NhaCungCap ncc)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.NhaCungCaps.InsertOnSubmit(ncc);

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool NhaCungCap_Xoa(NhaCungCap ncc)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhaCungCap nc = db.NhaCungCaps.Single(p => p.MaNhaCungCap == ncc.MaNhaCungCap);

                nc.NhaCungCap_TrangThai = "Khong su dung";

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool NhaCungCap_CapNhat(NhaCungCap ncc)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhaCungCap nc = db.NhaCungCaps.First(p => p.MaNhaCungCap == ncc.MaNhaCungCap);

                nc.NhaCungCap_Ten = ncc.NhaCungCap_Ten;
                nc.NhaCungCap_SoDienThoai = ncc.NhaCungCap_SoDienThoai;
                nc.NhaCungCap_DiaChi = ncc.NhaCungCap_DiaChi;
                nc.NhaCungCap_Email = ncc.NhaCungCap_Email;
                nc.NhaCungCap_Website = ncc.NhaCungCap_Website;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<NhaCungCap> NhaCungCap_TraCuu(NhaCungCap ncc)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                if (ncc.MaNhaCungCap == -1)
                {
                    var listNCC = from p in db.NhaCungCaps
                                  where p.NhaCungCap_TrangThai == "Su dung"
                                  select p;

                    return listNCC.ToList();
                }
                else
                {
                    var listNCC = from p in db.NhaCungCaps
                                  where p.NhaCungCap_TrangThai == "Su dung"
                                  select p;

                    return listNCC.ToList();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }


        [WebMethod]
        public bool NhanVien_Them(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.NhanViens.InsertOnSubmit(nv);

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool NhanVien_Xoa(NhanVien nv)
        {
            LoaiNhanVien l = NhanVien_LayLoaiNV(nv);
            if (l.LoaiNhanVien_Ten == "Chủ cửa hàng")
                return false;
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhanVien n = db.NhanViens.Single(p => p.MaNhanVien == nv.MaNhanVien);

                n.NhanVien_TrangThai = "Khong su dung";

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool NhanVien_CapNhat(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhanVien n = db.NhanViens.Single(p => p.MaNhanVien == nv.MaNhanVien);

                n.MaLoaiNhanVien = nv.MaLoaiNhanVien;
                n.NhanVien_DiaChi = nv.NhanVien_DiaChi;
                n.NhanVien_Email = nv.NhanVien_Email;
                n.NhanVien_HinhAnh = nv.NhanVien_HinhAnh;
                n.NhanVien_Ho = nv.NhanVien_Ho;
                n.NhanVien_Luong = nv.NhanVien_Luong;
                //n.NhanVien_MatKhau = nv.NhanVien_MatKhau;
                n.NhanVien_SoDienThoai = nv.NhanVien_SoDienThoai;
                n.NhanVien_Ten = n.NhanVien_Ten;
                n.NhanVien_TenDayDu = n.NhanVien_TenDayDu;
                //n.NhanVien_Phai = n.NhanVien_Phai;
                n.NhanVien_TenLot = nv.NhanVien_TenLot;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public bool NhanVien_DoiMatKhau(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhanVien n = db.NhanViens.Single(p => p.MaNhanVien == nv.MaNhanVien);

                n.NhanVien_MatKhau = nv.NhanVien_MatKhau;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<NhanVien> NhanVien_TraCuu(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listNV = from p in db.NhanViens
                             where p.NhanVien_TrangThai == "Su dung"
                             select p;
                return listNV.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public bool NhanVien_KiemTraTenDangNhap(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listNV = from p in db.NhanViens
                             where p.NhanVien_TenDangNhap == nv.NhanVien_TenDangNhap
                             select p;
                if (listNV.Count() == 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }
        [WebMethod]
        public NhanVien NhanVien_LayNV(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                NhanVien n = db.NhanViens.First(p => p.MaNhanVien == nv.MaNhanVien);
                return n;
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public NhanVien NhanVien_LayNVBangTenDangNhap(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listNV = from p in db.NhanViens
                             where p.NhanVien_TenDangNhap == nv.NhanVien_TenDangNhap
                             select p;

                if (listNV.Count() != 1)
                    return null;
                List<NhanVien> list = listNV.ToList();
                NhanVien n = list[0];
                return n;
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public LoaiNhanVien NhanVien_LayLoaiNV(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                LoaiNhanVien lnv = db.LoaiNhanViens.First(p => p.MaLoaiNhanVien == nv.MaLoaiNhanVien);

                return lnv;
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public bool NhanVien_KiemTraDangNhap(NhanVien nv)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listNV = from p in db.NhanViens
                             where p.NhanVien_TenDangNhap == nv.NhanVien_TenDangNhap & p.NhanVien_TrangThai == "Su dung"
                             select p;

                if (listNV.Count() != 1)
                    return false;
                List<NhanVien> list = listNV.ToList();
                NhanVien n = list[0];
                if (n.NhanVien_MatKhau == nv.NhanVien_MatKhau)
                    return true;
                return false;
            }
            catch (System.Exception ex)
            {
                return false;
            }
            return false;
        }

        [WebMethod]
        public List<LoaiNhanVien> LoaiNhanVien_LayDanhSach()
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listLNV = from p in db.LoaiNhanViens
                             where p.LoaiNhanVien_TrangThai == "Su dung"
                             select p;

                return listLNV.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        [WebMethod]
        public bool HoaDonBanHang_Them(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.HoaDonBanHangs.InsertOnSubmit(hdbh);

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        
        [WebMethod]
        public HoaDonBanHang HoaDonBanHang_LayHDGanNhat()
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listHDBH = from p in db.HoaDonBanHangs
                               where p.HDBH_TrangThai == "Su dung"
                               select p;
                List<HoaDonBanHang> list = listHDBH.ToList();
                HoaDonBanHang hdbh = list.Last();

                return hdbh;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
        [WebMethod]
        public bool HoaDonBanHang_Xoa(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                HoaDonBanHang hd = db.HoaDonBanHangs.First(p => p.MaHoaDon == hdbh.MaHoaDon);

                hd.HDBH_TrangThai = "Khong su dung";

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public List<HoaDonBanHang> HoaDonBanHang_TraCuu(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listHDBH = from p in db.HoaDonBanHangs
                               where p.HDBH_TrangThai == "Su dung"
                               select p;

                return listHDBH.ToList();

            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Them(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.ChiTietHoaDonBanHangs.InsertOnSubmit(cthdbh);

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Xoa(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                ChiTietHoaDonBanHang ct = db.ChiTietHoaDonBanHangs.First(p => p.MaCTHDBH == cthdbh.MaCTHDBH);

                ct.CTHDBH_TrangThai = "Khong su dung";

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_CapNhat(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                ChiTietHoaDonBanHang ct = db.ChiTietHoaDonBanHangs.First(p => p.MaCTHDBH == cthdbh.MaCTHDBH);

                ct.CTHDBH_SoLuong = cthdbh.CTHDBH_SoLuong;
                ct.CTHDBH_DonGia = cthdbh.CTHDBH_DonGia;

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public List<ChiTietHoaDonBanHang> ChiTietHoaDonBanHang_TraCuu(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();
                var listCTHDBH = from p in db.ChiTietHoaDonBanHangs
                                 where p.CTHDBH_TrangThai == "Su dung" & p.MaHoaDon == hdbh.MaHoaDon
                                 select p;

                return listCTHDBH.ToList();
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }




        /*[WebMethod]
        public bool HoaDonMuaHang_Them(HoaDonMuaHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.HoaDonBanHangs.InsertOnSubmit(hdbh);

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool HoaDonBanHang_Xoa(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                HoaDonBanHang hd = db.HoaDonBanHangs.First(p => p.MaHoaDon == hdbh.MaHoaDon);

                hd.HDBH_TrangThai = "Khong su dung";

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public List<HoaDonBanHang> HoaDonBanHang_TraCuu(HoaDonBanHang hdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                var listHDBH = from p in db.HoaDonBanHangs
                               where p.HDBH_TrangThai == "Su dung"
                               select p;

                return listHDBH.ToList();

            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Them(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                db.ChiTietHoaDonBanHangs.InsertOnSubmit(cthdbh);

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Xoa(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                ChiTietHoaDonBanHang ct = db.ChiTietHoaDonBanHangs.First(p => p.MaCTHDBH == cthdbh.MaCTHDBH);

                ct.CTHDBH_TrangThai = "Khong su dung";

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_CapNhat(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();

                ChiTietHoaDonBanHang ct = db.ChiTietHoaDonBanHangs.First(p => p.MaCTHDBH == cthdbh.MaCTHDBH);

                ct.CTHDBH_SoLuong = cthdbh.CTHDBH_SoLuong;
                ct.CTHDBH_DonGia = cthdbh.CTHDBH_DonGia;

                db.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public List<ChiTietHoaDonBanHang> ChiTietHoaDonBanHang_TraCuu(ChiTietHoaDonBanHang cthdbh)
        {
            try
            {
                QLSQADataContext db = new QLSQADataContext();
                var listCTHDBH = from p in db.ChiTietHoaDonBanHangs
                                 where p.CTHDBH_TrangThai == "Su dung"
                                 select p;

                return listCTHDBH.ToList();
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }*/
    }
}
